using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using JigLibX.Physics;
using Microsoft.Xna.Framework.Input;
using sqengine.Components;
using sqengine.Engine.Common;

namespace sqengine
{
    public class FrameRateCounter : DrawableGameComponent
    {

        SpriteBatch spriteBatch;
        SpriteFont spriteFont, spriteFont2;

        PhysicsSystem physics;
        AdvancedContent _adcontent;

        int frameRate = 0;
        int frameCounter = 0;
        TimeSpan elapsedTime = TimeSpan.Zero;
        bool _viewhelp;
        int bbWidth, bbHeight;
        Game _game;

        public FrameRateCounter(Game game, PhysicsSystem physics, AdvancedContent adcontent)
            : base(game)
        {

            this.physics = physics;
            this._adcontent = adcontent;
            this._game = game;
        }

        private void GraphicsDevice_DeviceReset(object sender, EventArgs e)
        {
            bbWidth = GraphicsDevice.PresentationParameters.BackBufferWidth;
            bbHeight = GraphicsDevice.PresentationParameters.BackBufferHeight;
        }

        protected override void LoadContent()
        {
            this.GraphicsDevice.DeviceReset += new EventHandler(GraphicsDevice_DeviceReset);
            GraphicsDevice_DeviceReset(null, null);

            spriteBatch = new SpriteBatch(GraphicsDevice);
            spriteFont = _adcontent.LoadSpriteFont("Fonts\\Miramo");
            spriteFont2 = _adcontent.LoadSpriteFont("Fonts\\Calibri");
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }


        public override void Update(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime;

            if (elapsedTime > TimeSpan.FromSeconds(1))
            {
                elapsedTime -= TimeSpan.FromSeconds(1);
                frameRate = frameCounter;
                frameCounter = 0;
            }

        }


        public void ShowHelp(bool state)
        {
            if (state)
            {
                _viewhelp = !_viewhelp;
            }

        }

        public bool DrawHelp { get; internal set; }

        public override void Draw(GameTime gameTime)
        {
            frameCounter++;

            string fps = frameRate.ToString();

            
            spriteBatch.Begin();
            spriteBatch.DrawString(spriteFont, fps, new Vector2(11, 6), Color.Black);
            spriteBatch.DrawString(spriteFont, fps, new Vector2(12, 7), Color.Yellow);

            spriteBatch.DrawString(spriteFont2, SqEngine.SqEngineInfo._name + " " + SqEngine.SqEngineInfo._version + " (press H for help)", new Vector2(0, bbHeight - 20), Color.Black);
            spriteBatch.DrawString(spriteFont2, SqEngine.SqEngineInfo._name + " " + SqEngine.SqEngineInfo._version + " (press H for help)", new Vector2(1, bbHeight - 19), Color.White);

            if (_viewhelp)
            {
                Texture2D tx = _adcontent.LoadTexture2D("Textures/UI/help");
                spriteBatch.Draw(tx,new Rectangle(0,0,tx.Width,tx.Height),Color.White);
            }
                spriteBatch.End();

            ((sqengine)this.Game).RestoreRenderState();
        }
    }
}
